package fr.upmc.i3s.fuglmeyeranalyser.view.menus;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import fr.upmc.i3s.fuglmeyeranalyser.MainWindow;
import fr.upmc.i3s.fuglmeyeranalyser.models.ModelEnum;
import fr.upmc.i3s.fuglmeyeranalyser.view.strings.StringsConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MenuCompute extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1836953794874823689L;

	// HMI contents
	private JTextField filePath;
	private JComboBox<String> modelingComboBox;
	private JComboBox<String> optimizationComboBox;
	private JButton btnStart;

	private MainWindow parent;

	/**
	 * Create the panel.
	 */
	public MenuCompute(MainWindow parent) {
		this.parent = parent;

		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		// Selected file label
		JLabel fileLabel = new JLabel(StringsConstants.LABEL_ANALYSED_FILE);
		add(fileLabel);
		filePath = new JTextField();
		springLayout.putConstraint(SpringLayout.SOUTH, fileLabel, -6, SpringLayout.NORTH, filePath);
		springLayout.putConstraint(SpringLayout.NORTH, filePath, 62, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, filePath, 315, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, filePath, 52, SpringLayout.WEST, this);
		filePath.setEditable(false);
		filePath.setText(MainWindow.getLoadedFileName());
		add(filePath);
		filePath.setColumns(10);

		// Select modeling
		JLabel modelingLabel = new JLabel(StringsConstants.LABEL_MODELING);
		springLayout.putConstraint(SpringLayout.NORTH, modelingLabel, 29, SpringLayout.SOUTH, filePath);
		springLayout.putConstraint(SpringLayout.WEST, modelingLabel, 29, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, fileLabel, 0, SpringLayout.WEST, modelingLabel);
		add(modelingLabel);
		modelingComboBox = new JComboBox<String>();
		modelingComboBox.addItem(ModelEnum.LINEAR.getLabel());
		modelingComboBox.addItem(ModelEnum.EXPONENTIEL.getLabel());
		springLayout.putConstraint(SpringLayout.NORTH, modelingComboBox, 6, SpringLayout.SOUTH, modelingLabel);
		springLayout.putConstraint(SpringLayout.WEST, modelingComboBox, 52, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, modelingComboBox, 0, SpringLayout.EAST, filePath);
		add(modelingComboBox);

		// Select optimization
		JLabel optimizationLabel = new JLabel(StringsConstants.LABEL_OPTIMIZATION);
		springLayout.putConstraint(SpringLayout.NORTH, optimizationLabel, 110, SpringLayout.SOUTH, filePath);
		springLayout.putConstraint(SpringLayout.WEST, optimizationLabel, 29, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, fileLabel, 0, SpringLayout.WEST, optimizationLabel);
		add(optimizationLabel);
		optimizationComboBox = new JComboBox<String>();
		optimizationComboBox.addItem(ModelEnum.MCO.getLabel());
		optimizationComboBox.addItem(ModelEnum.MCP.getLabel());
		optimizationComboBox.addItem(ModelEnum.MCPK.getLabel());
		springLayout.putConstraint(SpringLayout.NORTH, optimizationComboBox, 10, SpringLayout.SOUTH, optimizationLabel);
		springLayout.putConstraint(SpringLayout.WEST, optimizationComboBox, 52, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, optimizationComboBox, 0, SpringLayout.EAST, filePath);
		add(optimizationComboBox);

		btnStart = new JButton(StringsConstants.BUTTON_NAME_START);
		springLayout.putConstraint(SpringLayout.NORTH, btnStart, 36, SpringLayout.SOUTH, optimizationComboBox);
		springLayout.putConstraint(SpringLayout.WEST, btnStart, 0, SpringLayout.WEST, filePath);
		btnStart.addActionListener(this);
		add(btnStart);

	}

	//retourn le chemin vers le fichier choisi par l'utilisateur.
	public String getFichierAnalizePath() {
		return filePath.getText();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnStart) {
			int modelSelect = modelingComboBox.getSelectedIndex();
			int optimSelect = optimizationComboBox.getSelectedIndex();
			ModelEnum model = ModelEnum.getModelByKey(modelSelect);
			ModelEnum optim = ModelEnum.getModelByKey(optimSelect+2);
			System.out.println("model = "+model+", optim = "+optim);
			parent.startCompute(model, optim);
		}
	}

}
